package com.liukai.todo_android.ui.adapter;

import android.content.ClipData;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.liukai.todo_android.R;
import com.liukai.todo_android.common.utils.TextViewBgUtil;
import com.liukai.todo_android.entity.ItemVo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：liukai
 * @date ：Created in 2021/8/10 3:33 下午
 * @description：待办事项列表的适配器
 * @modified By：
 * @version: $
 */
public class ToDoRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private MyToDoViewHolder myToDoViewHolder;
    private final Context context;
    private List<ItemVo> itemVoList;

    /**
     * 查找所有方式
     */
    public static final int STYLE_FIND_ALL = 0;
    /**
     * 搜索方式
     */
    public static final int STYLE_SEARCH = 1;
    public int style = STYLE_FIND_ALL;
    public String keyWord = "";

    public ToDoRecyclerViewAdapter(Context _context) {
        this(_context, new ArrayList<>());
    }

    public ToDoRecyclerViewAdapter(Context _context, List<ItemVo> itemVoList) {
        context = _context;
        this.itemVoList = itemVoList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_recycler_view_todo, null);
        return new MyToDoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        MyToDoViewHolder myToDoViewHolder = (MyToDoViewHolder) holder;
        ItemVo itemVo = itemVoList.get(position);

        myToDoViewHolder.todoName.setText(itemVo.getTitle());
        onBindTextView(myToDoViewHolder.todoName, itemVo.getTitle());
        if (itemVo.getDescription() != null && !"".equals(itemVo.getDescription())) {
            myToDoViewHolder.todoDesc.setVisibility(View.VISIBLE);
            myToDoViewHolder.todoDesc.setText(itemVo.getDescription());
            onBindTextView(myToDoViewHolder.todoDesc, itemVo.getDescription());
        } else {
            myToDoViewHolder.todoDesc.setVisibility(View.GONE);
        }
        myToDoViewHolder.btnConcentration.setOnClickListener(v -> {
            onItemClick.concentrationClick(itemVo);
        });

        myToDoViewHolder.btnMore.setOnClickListener(v -> {
            onItemClick.moreClick(myToDoViewHolder.btnMore, itemVo);
        });

        myToDoViewHolder.itemView.setOnClickListener(v -> {
            onItemClick.itemClick(itemVo);
        });

    }

    /**
     * 绑定TextView
     *
     * @param textView 文本框组件
     * @param text     文本框内容
     */
    private void onBindTextView(TextView textView, String text) {
        textView.setText(text);
        if (style == STYLE_SEARCH && !"".equals(keyWord) && text.contains(keyWord)) {
            TextViewBgUtil.setTextBg(textView, text, keyWord, context);
        }
    }

    @Override
    public int getItemCount() {
        return itemVoList.size();
    }

    /**
     * 设置todo列表
     *
     * @param itemVoList
     */
    public void setScheduleList(List<ItemVo> itemVoList, int style, String keyWord) {
        this.itemVoList = itemVoList;
        this.style = style;
        this.keyWord = keyWord;
        notifyDataSetChanged();
    }

    static class MyToDoViewHolder extends RecyclerView.ViewHolder {

        public TextView todoName;
        public TextView todoDesc;
        public TextView btnMore;
        public TextView btnConcentration;

        public MyToDoViewHolder(@NonNull View itemView) {
            super(itemView);
            todoName = itemView.findViewById(R.id.todo_name);
            todoDesc = itemView.findViewById(R.id.todo_desc);
            btnMore = itemView.findViewById(R.id.btn_more);
            btnConcentration = itemView.findViewById(R.id.btn_concentration);
        }
    }

    public interface OnItemClick {
        /**
         * 更多按钮
         *
         * @param view
         * @param itemVo
         */
        void moreClick(View view, ItemVo itemVo);

        /**
         * 专注
         *
         * @param itemVo
         */
        void concentrationClick(ItemVo itemVo);

        /**
         * 列表项
         *
         * @param itemVo
         */
        void itemClick(ItemVo itemVo);
    }

    private OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }


}
